Skip to content

Commit 53e6688

Browse files
committed
rewrote dependencies downloader
1 parent cd167fb commit 53e6688

File tree

9 files changed

+164
-142
lines changed

9 files changed

+164
-142
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ install:
8282
- pip install -U pip
8383
- pip install -U -r dev_requirements.txt
8484
# Java
85-
- python open_fortran_parser/dev_dependencies.py
85+
- python -m open_fortran_parser --dev-deps
8686
- export CLASSPATH="${CLASSPATH}:$(pwd)/lib/*"
8787
- ant
8888
- export CLASSPATH="${CLASSPATH}:$(pwd)/dist/*"

README.rst

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ Get dependencies, either manually, or using the provided script:
8484

8585
.. code:: bash
8686
87-
python3 open_fortran_parser/dev_dependencies.py
87+
pip3 install -U -r requirements.txt
88+
python3 -m open_fortran_parser --dev-deps
8889
export CLASSPATH="${CLASSPATH}:$(pwd)/lib/*"
8990
9091
Build:
@@ -217,52 +218,78 @@ Python version >= 3.6.
217218

218219
Python libraries as specified in `<requirements.txt>`_.
219220

220-
Building and running tests additionally requires packages listed in `<dev_requirements.txt>`_.
221+
Building and running tests additionally requires packages listed in `<test_requirements.txt>`_.
221222

222223

223224
how to build
224225
------------
225226

226227
.. code:: bash
227228
228-
pip3 install -U -r dev_requirements.txt
229+
pip3 install -U -r test_requirements.txt
229230
python3 setup.py sdist --formats=gztar,zip
230231
python3 setup.py bdist_wheel
231232
233+
how to install
234+
--------------
235+
236+
You can simply install from PyPI:
237+
238+
.. code:: bash
239+
240+
pip3 install open_fortran parser
241+
242+
Or using any of below commands, when installing from source:
243+
244+
.. code:: bash
245+
246+
pip3 install .
247+
pip3 install dist/<filename>.whl
248+
pip3 install dist/<filename>.tar.gz
249+
pip3 install dist/<filename>.zip
250+
232251
233252
how to run
234253
----------
235254

236255
The wrapper can be used as a script, or as a library.
237256

238-
Before running, however, please make sure that dependencies are configured correctly.
239-
You can do that by either following the "how to build" section for Java implementation above,
240-
or by executing this:
257+
When running any installed version, even if installed from source, dependencies are automatically
258+
installed together with the wrapper.
259+
260+
Before running from source (without installation), however, please follow "how to build" section
261+
for Java implementation above.
262+
You can make sure that dependencies are configured correctly by running:
241263

242264
.. code:: bash
243265
244-
python3 open_fortran_parser/dependencies.py
245-
export CLASSPATH="${CLASSPATH}:$(pwd)/lib/*"
266+
python3 -m open_fortran_parser --deps
267+
246268
247269
as script
248270
~~~~~~~~~
249271

250272
.. code:: bash
251273
252274
$ python3 -m open_fortran_parser -h
253-
usage: open_fortran_parser [-h] [-v VERBOSITY] input [output]
275+
usage: open_fortran_parser [-h] [--version] [-v VERBOSITY]
276+
[--get-dependencies]
277+
[input] [output]
254278
255-
Python wrapper around XML generator for Open Fortran Parser 0.8.4
279+
Python wrapper around XML generator for Open Fortran Parser
256280
257281
positional arguments:
258-
input path to Fortran source code file
282+
input path to Fortran source code file (default: None)
259283
output writable path for where to store resulting XML,
260284
defaults to stdout if no path provided (default: None)
261285
262286
optional arguments:
263287
-h, --help show this help message and exit
288+
--version show program's version number and exit
264289
-v VERBOSITY, --verbosity VERBOSITY
265290
level of verbosity, from 0 to 100 (default: 100)
291+
--get-dependencies, --deps
292+
download dependencies and exit (default: False)
266293
267294
Copyright 2017 Mateusz Bysiek https://mbdevpl.github.io/, Apache License 2.0
268295
@@ -285,6 +312,6 @@ testing
285312
python3 -m pylint --load-plugins=pylint.extensions.mccabe --docstring-min-length 5 \
286313
--no-docstring-rgx "^(test)?_|.*Tests$" --unsafe-load-any-extension y \
287314
--output-format colorized --reports y $(find . -name "*.py")
288-
python3 -m coverage run --branch -m unittest discover --verbose
315+
python3 -m coverage run --branch --source . -m unittest discover --verbose
289316
python3 -m coverage report --show-missing
290317
python3 -m coverage html

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ install:
3838
- pip install -U pip
3939
- pip install -U -r dev_requirements.txt
4040
# Java
41-
- python open_fortran_parser\\dev_dependencies.py
41+
- python -m open_fortran_parser --dev-deps
4242
- set CLASSPATH=%cd%\\lib\\*;%CLASSPATH%
4343
- ant
4444
- set CLASSPATH=%cd%\\dist\\*;%CLASSPATH%

open_fortran_parser/config.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import pathlib
44

5+
from .dependencies import DEPENDENCIES_PATH
6+
57
JAVA = {
68
'executable': pathlib.Path('java'),
7-
'classpath': None,
9+
'classpath': pathlib.Path(DEPENDENCIES_PATH, '*'),
810
'options': None,
911
'ofp_class': 'fortran.ofp.FrontEnd',
1012
'ofp_xml_class': 'fortran.ofp.XMLPrinter'}

open_fortran_parser/dependencies.py

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,76 @@
22

33
"""Dependency downloader for open_fortran_parser."""
44

5+
import logging
6+
import os
57
import pathlib
8+
import platform
9+
import typing as t
610
import urllib
711

8-
if __name__ == '__main__':
9-
try:
10-
from .dev_dependencies import DEV_DEPENDENCIES, ensure_dependencies
11-
except ImportError:
12-
from dev_dependencies import DEV_DEPENDENCIES, ensure_dependencies
13-
else:
14-
from .dev_dependencies import DEV_DEPENDENCIES, ensure_dependencies
12+
import wget
13+
14+
try:
15+
from ._version import VERSION
16+
except SystemError:
17+
from _version import VERSION
18+
19+
_LOG = logging.getLogger(__name__)
20+
21+
DEV_DEPENDENCIES = {
22+
'ANTLR 3.3': (
23+
urllib.parse.urlparse(
24+
'https://github.com/mbdevpl/open-fortran-parser/releases/download/v0.8.4-1/'),
25+
pathlib.Path('antlr-3.3-complete.jar')),
26+
'Open Fortran Parser 0.8.4-2': (
27+
urllib.parse.urlparse(
28+
'https://github.com/mbdevpl/open-fortran-parser/releases/download/v0.8.4-2/'),
29+
pathlib.Path('OpenFortranParser-0.8.4-2.jar')),
30+
'Apache Commons CLI 1.4': (
31+
urllib.parse.urlparse(
32+
'https://github.com/mbdevpl/open-fortran-parser-xml/releases/download/v0.1.0/'),
33+
pathlib.Path('commons-cli-1.4.jar'))}
34+
35+
DEV_DEPENDENCIES_PATH = pathlib.Path(os.getcwd(), 'lib')
1536

1637
DEPENDENCIES = DEV_DEPENDENCIES.copy()
1738

1839
DEPENDENCIES.update({
19-
'Open Fortran Parser XML 0.2.0': (
40+
'Open Fortran Parser XML {}'.format(VERSION): (
2041
urllib.parse.urlparse(
21-
'https://github.com/mbdevpl/open-fortran-parser-xml/releases/download/v0.2.0/'),
22-
pathlib.Path('OpenFortranParserXML-0.2.0.jar'))})
42+
'https://github.com/mbdevpl/open-fortran-parser-xml/releases/download/v{}/'
43+
.format(VERSION)),
44+
pathlib.Path('OpenFortranParserXML-{}.jar'.format(VERSION)))})
45+
46+
DEPENDENCIES_PATH = pathlib.Path(__file__).resolve().parent
47+
48+
OUTDATED_DEPENDENCIES = {
49+
'Open Fortran Parser 0.8.4-1': pathlib.Path('OpenFortranParser-0.8.4-2.jar')}
50+
2351

52+
def ensure_dependencies(
53+
dependencies: t.Mapping[str, t.Tuple[urllib.parse.ParseResult, pathlib.Path]],
54+
target_dir: pathlib.Path, silent: bool = False) -> None:
55+
"""Download missing depenedencies."""
56+
if not target_dir.exists():
57+
_LOG.warning('Creating directory "%s"...', target_dir)
58+
os.makedirs(str(target_dir), exist_ok=True)
59+
for dependency, (url_root, filename) in dependencies.items():
60+
path = target_dir.joinpath(filename)
61+
if path.is_file():
62+
_LOG.warning('%s is present already.', dependency)
63+
continue
64+
url = urllib.parse.urlunparse(url_root) + str(filename)
65+
_LOG.warning('Downloading %s from URL "%s" to path "%s"...', dependency, url, path)
66+
wget.download(url, str(path), bar=None if silent else wget.bar_adaptive)
67+
if not silent:
68+
print()
69+
if not silent:
70+
classpath = target_dir.joinpath('*')
71+
_LOG.warning('If you wish to use the Open Fortran Parser XML generator directly,'
72+
' please add "%s" to your Java classpath:', classpath)
73+
if platform.system() != 'Windows':
74+
_LOG.warning('export CLASSPATH="${CLASSPATH}:%s"', classpath)
2475

2576
if __name__ == '__main__':
26-
ensure_dependencies(DEPENDENCIES)
77+
ensure_dependencies(DEPENDENCIES, DEPENDENCIES_PATH)

open_fortran_parser/dev_dependencies.py

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

open_fortran_parser/main.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
import argparse
44
import logging
55
import pathlib
6+
import sys
67

8+
from ._version import VERSION
79
from .parser_wrapper import execute_parser
10+
from .dependencies import \
11+
DEV_DEPENDENCIES, DEV_DEPENDENCIES_PATH, DEPENDENCIES, DEPENDENCIES_PATH, ensure_dependencies
812

913
logging.basicConfig()
1014

@@ -15,20 +19,42 @@ def main(args=None, namespace=None):
1519
"""Launch Open Fortran Parser."""
1620
parser = argparse.ArgumentParser(
1721
prog='open_fortran_parser',
18-
description='''Python wrapper around XML generator for Open Fortran Parser 0.8.4''',
22+
description='''Python wrapper around XML generator for Open Fortran Parser''',
1923
epilog='''Copyright 2017 Mateusz Bysiek https://mbdevpl.github.io/, Apache License 2.0''',
2024
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
2125

22-
parser.add_argument('input', type=pathlib.Path, help='''path to Fortran source code file''')
26+
parser.version = VERSION
27+
parser.add_argument('--version', action='version')
28+
29+
parser.add_argument(
30+
'input', nargs='?', type=pathlib.Path, help='''path to Fortran source code file''')
2331
parser.add_argument(
2432
'output', nargs='?', type=pathlib.Path, default=None,
2533
help='''writable path for where to store resulting XML, defaults to stdout
2634
if no path provided''')
2735
parser.add_argument(
2836
'-v', '--verbosity', type=int, default=100, help='''level of verbosity, from 0 to 100''')
37+
parser.add_argument(
38+
'--get-dependencies', '--deps', action='store_true',
39+
help='''download dependencies and exit''')
40+
parser.add_argument(
41+
'--get-development-dependencies', '--dev-deps', action='store_true',
42+
help=argparse.SUPPRESS)
2943

3044
args = parser.parse_args(args, namespace)
3145

46+
if args.get_development_dependencies:
47+
ensure_dependencies(DEV_DEPENDENCIES, DEV_DEPENDENCIES_PATH)
48+
return
49+
50+
if args.get_dependencies:
51+
ensure_dependencies(DEPENDENCIES, DEPENDENCIES_PATH)
52+
return
53+
54+
if not args.input:
55+
parser.print_help(sys.stderr)
56+
parser.exit(2)
57+
3258
process = execute_parser(args.input, args.output, args.verbosity)
3359
if process.stderr:
3460
_LOG.warning(process.stderr.decode().rstrip())

0 commit comments

Comments
 (0)