Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

run.py 1.8 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  1. """
  2. Helper to set up environments & run book data tools properly.
  3. Most DVC stages will use this to actually run the code. It makes
  4. sure we compile the Rust tools, routes arguments properly, and sets
  5. environment variables that may be needed. For Python scripts, it
  6. ensures the search path is set correctly.
  7. Usage:
  8. run.py --rust TOOL ARGS...
  9. run.py SCRIPT ARGS...
  10. Options:
  11. --rust
  12. Run a Rust tool instead of a Python script.
  13. TOOL
  14. The name of the Rust tool to run
  15. SCRIPT
  16. The name of the Python script to run.
  17. ARGS
  18. The arguments to the tool or script.
  19. """
  20. import os
  21. import os.path
  22. import sys
  23. import runpy
  24. from pathlib import Path
  25. import logging
  26. import subprocess as sp
  27. _log = logging.getLogger('run.py')
  28. src_dir = Path(__file__).parent
  29. sys.path.insert(0, src_dir)
  30. from bookdata import setup, bin_dir
  31. def run_rust():
  32. # this is a rust command
  33. del sys.argv[1]
  34. # we need to fix up Rust environment in some cases
  35. sysroot = os.environ.get('CONDA_BUILD_SYSROOT', None)
  36. if sysroot and 'RUSTFLAGS' not in os.environ:
  37. _log.info('setting Rust flags from sysroot')
  38. os.environ['RUSTFLAGS'] = f'-L native={sysroot}/usr/lib64 -L native={sysroot}/lib64'
  39. # shell out to 'cargo run' to run the command
  40. tool_name = sys.argv[1]
  41. _log.info('building and running Rust tool %s', tool_name)
  42. sp.run(['cargo', 'run', '--release', '--'] + sys.argv[1:], check=True)
  43. def run_script():
  44. script = sys.argv[1]
  45. del sys.argv[1]
  46. if os.path.exists(script):
  47. _log.info('running %s', script)
  48. runpy.run_path(script)
  49. else:
  50. _log.info('preparing to run scripts.%s', script)
  51. runpy.run_module(f'scripts.{script}', alter_sys=True)
  52. if __name__ == '__main__':
  53. setup()
  54. if sys.argv[1] == '--rust':
  55. run_rust()
  56. else:
  57. run_script()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...