Skip to content

Commit 5b8a69e

Browse files
committed
rebuild project to a python executable module.
1 parent e1b6113 commit 5b8a69e

File tree

9 files changed

+228
-89
lines changed

9 files changed

+228
-89
lines changed

README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,31 @@
1-
# Java Project Builder
1+
# Java Project Builder
2+
1. describe
3+
```
4+
JPB(java project builder) is a simple tool for building java project which means
5+
you can use it to compile or execute java project only use one line command. The
6+
only thing you have do is to point at which source file to compile or which source
7+
file is the main file. Hope this tool can take little convenient to you in java
8+
project building !
9+
```
10+
---
11+
2. usage
12+
```
13+
usage: jpb [-h] [-c] [-e] [-p PROFILE] [-r PROROOT] mainfile
14+
positional arguments:
15+
mainfile main file to build.
16+
optional arguments:
17+
-h, --help show this help message and exit
18+
-c, --compile choose compile mode.
19+
-e, --execute choose execute mode.
20+
-p PROFILE, --profile PROFILE
21+
project file to mark workspace. default is
22+
`java.project`.
23+
-r PROROOT, --proroot PROROOT
24+
point at default project root `pwd`、`cwd` or given
25+
realpath. default is `cwd`.
26+
```
27+
---
28+
3. demo
229
+ execute compiled class file:
330
``` python3 jpb.py -e [class file path]```
431
+ compile java project:

jpb/Hello.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class Hello {
2+
public static void main(String[] args){
3+
System.out.println("Hello, World !");
4+
}
5+
}

jpb/__init__.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from __future__ import absolute_import
2+
import os
3+
import sys
4+
import argparse
5+
import jpb.jpblib as jpblib
6+
7+
8+
def main():
9+
parser = argparse.ArgumentParser()
10+
parser.add_argument('-c', '--compile', help='choose compile mode.', action='store_true')
11+
parser.add_argument('-e', '--execute', help='choose execute mode.', action='store_true')
12+
parser.add_argument('-p', '--profile', help='project file to mark workspace. default is\
13+
`java.project`.', default='java.project', type=str)
14+
parser.add_argument('-r', '--proroot', help='point at default project root `pwd`、`cwd`\
15+
or given realpath. default is `cwd`.', default='cwd', type=str)
16+
parser.add_argument('mainfile', help='main file to build.', type=str)
17+
args = parser.parse_args()
18+
if os.path.isfile(args.mainfile):
19+
project_root = jpblib.find_project_root(args.profile, args.mainfile, args.proroot)
20+
classpath = jpblib.get_classpath(project_root, args.mainfile)
21+
if args.compile:
22+
print('----------java project builder (compile mode)----------')
23+
jpblib.compile_class_file(project_root, classpath)
24+
if args.execute:
25+
print('----------java project builder (execute mode)----------')
26+
jpblib.execute_main_class(project_root, classpath)
27+
else:
28+
print('File "%s" not found !' % args.mainfile)
29+
30+
if __name__ == '__main__':
31+
sys.exit(main())

jpb/__main__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from __future__ import absolute_import
2+
import os
3+
import sys
4+
5+
6+
if __package__ == '':
7+
# __file__ is pip-*.whl/pip/__main__.py
8+
# first dirname call strips of '/__main__.py', second strips off '/pip'
9+
# Resulting path is the name of the wheel itself
10+
# Add that to sys.path so we can import pip
11+
path = os.path.dirname(os.path.dirname(__file__))
12+
sys.path.insert(0, path)
13+
14+
import jpb
15+
16+
if __name__ == '__main__':
17+
sys.exit(jpb.main())

jpb/java.project

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# This is the marker file of root directory.

jpb/jpb.py

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
#!/usr/bin/env python3
2+
3+
'''
4+
Auto compile or execute java project by setting root mark file and given java
5+
file path.
6+
7+
usage: jpb [-h] [-c] [-e] [-p PROFILE] [-r PROROOT] mainfile
8+
9+
positional arguments:
10+
mainfile main file to build.
11+
12+
optional arguments:
13+
-h, --help show this help message and exit
14+
-c, --compile choose compile mode.
15+
-e, --execute choose execute mode.
16+
-p PROFILE, --profile PROFILE
17+
project file to mark workspace. default is
18+
`java.project`.
19+
-r PROROOT, --proroot PROROOT
20+
point at default project root `pwd`、`cwd` or given
21+
realpath. default is `cwd`.
22+
'''
23+
24+
import os
25+
import argparse
26+
27+
def find_project_root(project_file, filepath, proroot='cwd'):
28+
'''
29+
Find project root dir accroding to mark-file name and given java file path.
30+
31+
Args:
32+
project_file [str] project root mark file `java.project`.You need to create
33+
it in root dir.
34+
35+
filepth [str] target compile or execute java file. The script will start
36+
to find mark-file from its parent dir.
37+
38+
return: [str] project root dir.
39+
'''
40+
def forward_walk(filepath):
41+
dirname = os.path.dirname(filepath)
42+
if dirname != os.path.join(os.path.splitdrive(dirname)[0], os.path.sep):
43+
yield dirname
44+
yield from forward_walk(os.path.dirname(filepath))
45+
filepath = os.path.abspath(filepath)
46+
if proroot == 'cwd':
47+
# set default project root to cwd.
48+
project_root = os.getcwd()
49+
elif proroot == 'pwd':
50+
# set default project root to file directory.
51+
project_root = os.path.dirname(filepath)
52+
else:
53+
# set default project root to pointed dir.
54+
project_root = os.path.abspath(proroot)
55+
for dirname in forward_walk(filepath):
56+
if os.path.isfile(os.path.join(dirname, project_file)):
57+
project_root = dirname
58+
break
59+
return project_root
60+
61+
def compile_class_file(project_root, classpath):
62+
'''
63+
Compile java file associated with given classpath to class file.
64+
65+
Args:
66+
project_root [str] jvm's workspace also project root dir.
67+
68+
classpath [str] target classpath to compile like `com.hello.Hello`.
69+
70+
return: [None]
71+
'''
72+
cwd_tmp = os.getcwd()
73+
os.chdir(project_root)
74+
os.system('javac -encoding UTF-8 -d . '+classpath.replace('.', os.path.sep)+'.java')
75+
os.chdir(cwd_tmp)
76+
77+
def execute_main_class(project_root, classpath):
78+
'''
79+
Execute compiled main class of project.
80+
81+
Args:
82+
project_root [str] jvm's workspace.
83+
84+
classpath [str] main class path like `com.hello.Hello`.
85+
86+
return: [None]
87+
'''
88+
cwd_tmp = os.getcwd()
89+
os.chdir(project_root)
90+
os.system('java '+classpath)
91+
os.chdir(cwd_tmp)
92+
93+
def get_classpath(project_root, filepath, ):
94+
'''
95+
Get standerd classpath from project dir and java file path.
96+
97+
Args:
98+
project_root [str] project root dir.
99+
100+
filepath [str] target java file path.
101+
102+
return: [str] standerd classpath of java file like `com.hello.Hello`.
103+
'''
104+
filepath = os.path.abspath(filepath)
105+
class_file = filepath.split(os.path.abspath(os.path.join(project_root,'%{mark}%')).split('%{mark}%')[0])[1]
106+
class_path = os.path.splitext(class_file)[0].replace(os.path.sep, '.')
107+
return class_path
108+
109+
def main():
110+
parser = argparse.ArgumentParser()
111+
parser.add_argument('-c', '--compile', help='choose compile mode.', action='store_true')
112+
parser.add_argument('-e', '--execute', help='choose execute mode.', action='store_true')
113+
parser.add_argument('-p', '--profile', help='project file to mark workspace. default is\
114+
`java.project`.', default='java.project', type=str)
115+
parser.add_argument('-r', '--proroot', help='point at default project root `pwd`、`cwd`\
116+
or given realpath. default is `cwd`.', default='cwd', type=str)
117+
parser.add_argument('mainfile', help='main file to build.', type=str)
118+
args = parser.parse_args()
119+
if os.path.isfile(args.mainfile):
120+
project_root = find_project_root(args.profile, args.mainfile, args.proroot)
121+
classpath = get_classpath(project_root, args.mainfile)
122+
if args.compile:
123+
print('----------java project builder (compile mode)----------')
124+
compile_class_file(project_root, classpath)
125+
if args.execute:
126+
print('----------java project builder (execute mode)----------')
127+
execute_main_class(project_root, classpath)
128+
else:
129+
print('File "%s" not found !' % args.mainfile)
130+
131+
if __name__ == '__main__':
132+
main()

jpb.py renamed to jpb/jpblib.py

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111
'''
1212

1313
import os
14-
import sys
1514

16-
def find_project_root(project_file, filepath):
15+
def find_project_root(project_file, filepath, proroot='cwd'):
1716
'''
1817
Find project root dir accroding to mark-file name and given java file path.
1918
@@ -31,7 +30,16 @@ def forward_walk(filepath):
3130
if dirname != os.path.join(os.path.splitdrive(dirname)[0], os.path.sep):
3231
yield dirname
3332
yield from forward_walk(os.path.dirname(filepath))
34-
project_root = os.path.dirname(filepath)
33+
filepath = os.path.abspath(filepath)
34+
if proroot == 'cwd':
35+
# set default project root to cwd.
36+
project_root = os.getcwd()
37+
elif proroot == 'pwd':
38+
# set default project root to file directory.
39+
project_root = os.path.dirname(filepath)
40+
else:
41+
# set default project root to pointed dir.
42+
project_root = os.path.abspath(proroot)
3543
for dirname in forward_walk(filepath):
3644
if os.path.isfile(os.path.join(dirname, project_file)):
3745
project_root = dirname
@@ -70,7 +78,7 @@ def execute_main_class(project_root, classpath):
7078
os.system('java '+classpath)
7179
os.chdir(cwd_tmp)
7280

73-
def get_classpath(project_root, filepath):
81+
def get_classpath(project_root, filepath, ):
7482
'''
7583
Get standerd classpath from project dir and java file path.
7684
@@ -81,32 +89,7 @@ def get_classpath(project_root, filepath):
8189
8290
return: [str] standerd classpath of java file like `com.hello.Hello`.
8391
'''
84-
class_file = filepath.split(project_root+os.sep)[1]
92+
filepath = os.path.abspath(filepath)
93+
class_file = filepath.split(os.path.abspath(os.path.join(project_root,'%{mark}%')).split('%{mark}%')[0])[1]
8594
class_path = os.path.splitext(class_file)[0].replace(os.path.sep, '.')
8695
return class_path
87-
88-
def show_help():
89-
print('''Usage: jpb [options] filename
90-
Options:
91-
-c only compile mode.
92-
-e only execute mode.''')
93-
94-
def main():
95-
if len(sys.argv)>1:
96-
filepath = os.path.abspath(sys.argv[-1])
97-
modelist = sys.argv[1:-1]
98-
if(os.path.isfile(filepath)):
99-
project_root = find_project_root('java.project', filepath)
100-
classpath = get_classpath(project_root, filepath)
101-
if '-c' in modelist or modelist==[]:
102-
print('----------java Project Builder (Mode C)----------')
103-
compile_class_file(project_root, classpath)
104-
if '-e' in modelist or modelist==[]:
105-
execute_main_class(project_root, classpath)
106-
else:
107-
print('File "%s" not found !' % filepath)
108-
else:
109-
show_help()
110-
111-
if __name__ == '__main__':
112-
main()

jpb_.py

Lines changed: 0 additions & 52 deletions
This file was deleted.

test.py

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)