|
5 | 5 |
|
6 | 6 | A module that contains helpful methods for other modules |
7 | 7 | """ |
| 8 | +import subprocess |
| 9 | +import logging |
| 10 | +import json |
| 11 | +from pathlib import Path |
| 12 | +from forklift import config |
8 | 13 |
|
9 | 14 |
|
10 | 15 | def format_time(seconds): |
@@ -39,3 +44,28 @@ def __enter__(self): |
39 | 44 |
|
40 | 45 | def __exit__(self, type, value, traceback): |
41 | 46 | self.pallet.stop_timer(self.name) |
| 47 | + |
| 48 | + |
| 49 | +def map_network_drive(name, drive_letter): |
| 50 | + parameters = json.load(Path(Path(config.config_location).parent, 'share', f'{name}.json').open('r')) |
| 51 | + path = parameters['path'] |
| 52 | + username = parameters['username'] |
| 53 | + password = parameters['password'] |
| 54 | + logger = logging.getLogger('forklift') |
| 55 | + if not drive_letter.endswith(':'): |
| 56 | + drive_letter += ':' |
| 57 | + logger.debug(f"Mapping network drive: {path} to {drive_letter}") |
| 58 | + try: |
| 59 | + result = subprocess.run( |
| 60 | + ["net", "use", drive_letter, path, password, f"/user:{username}", '/persistent:yes'], |
| 61 | + check=True, |
| 62 | + stdout=subprocess.PIPE, |
| 63 | + stderr=subprocess.PIPE, |
| 64 | + text=True, |
| 65 | + ) |
| 66 | + logger.info(f"Network share mounted successfully: {result.stdout.strip()}") |
| 67 | + except subprocess.CalledProcessError as e: |
| 68 | + if '85' in e.stderr or '1219' in e.stderr: |
| 69 | + logger.debug('ignoring error 85, drive already mapped') |
| 70 | + else: |
| 71 | + raise Exception(f"Error mounting network share: {e.stderr.strip()}") from e |
0 commit comments