Skip to content

Commit f97581d

Browse files
committed
feat: add utility function for mapping network drives
1 parent 85cc297 commit f97581d

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,14 @@ From within the [ArcGIS Pro conda environment](http://pro.arcgis.com/en/pro-app/
148148
}
149149
```
150150

151+
1. Add network drive mapping config to `<garage folder>/share`. For example:
152+
```json
153+
{
154+
"path": "...",
155+
"username": "...",
156+
"password": "..."
157+
}
158+
151159
1. `forklift lift`
152160
1. `forklift ship`
153161

src/forklift/seat.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
66
A module that contains helpful methods for other modules
77
"""
8+
import subprocess
9+
import logging
10+
import json
11+
from pathlib import Path
12+
from forklift import config
813

914

1015
def format_time(seconds):
@@ -39,3 +44,28 @@ def __enter__(self):
3944

4045
def __exit__(self, type, value, traceback):
4146
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

Comments
 (0)