dcp-client 5.8.5 → 5.8.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/fs-basic.py CHANGED
@@ -6,6 +6,8 @@
6
6
 
7
7
  import os
8
8
  import sys
9
+ import tempfile
10
+ import errno
9
11
  import pythonmonkey as pm
10
12
 
11
13
  def readFile(filename: str) -> str:
@@ -16,6 +18,46 @@ def writeFile(filename: str, contents: str) -> None:
16
18
  with open(filename, "w", encoding="utf8") as fileHnd:
17
19
  fileHnd.write(contents)
18
20
 
21
+ def writeFileAtomic(filename: str, contents, options = None) -> bool:
22
+ if isinstance(contents, str):
23
+ data = contents.encode('utf-8')
24
+ else:
25
+ data = contents
26
+
27
+ writeDir = os.path.dirname(filename)
28
+ fd, tempFilename = tempfile.mkstemp(prefix=os.path.basename(filename) + '-', dir=writeDir)
29
+
30
+ try:
31
+ # The temp file must be closed before link or replace
32
+ with os.fdopen(fd, 'wb') as fileHnd:
33
+ fileHnd.write(data)
34
+ fileHnd.flush()
35
+ os.fsync(fileHnd.fileno())
36
+ # Overwrite by default. Mimics how the JS version options handling
37
+ if not isinstance(options, dict) or options.get('overwrite') is not False:
38
+ os.replace(tempFilename, filename)
39
+ else:
40
+ # Lean on link(2) atomic guarantees to avoid overwriting the file
41
+ # We cannot use os.rename since some operating systems don't perform it atomically
42
+ try:
43
+ os.link(tempFilename, filename)
44
+ except FileExistsError:
45
+ return False
46
+ except OSError as error:
47
+ if error.errno != errno.EACCES:
48
+ raise
49
+ # Hard links are unavailable, so we fallback to check and replace
50
+ if os.path.exists(filename):
51
+ return False
52
+ os.replace(tempFilename, filename)
53
+ return True
54
+ finally:
55
+ # Unlinking will delete the temp file. We don't care if it was already deleted
56
+ try:
57
+ os.unlink(tempFilename)
58
+ except FileNotFoundError:
59
+ pass
60
+
19
61
  def getMode(filename: str) -> int:
20
62
  return os.stat(filename).st_mode
21
63
 
@@ -30,6 +72,7 @@ def mkdir(dirname: str) -> None:
30
72
 
31
73
  exports['readFile'] = readFile
32
74
  exports['writeFile'] = writeFile
75
+ exports['writeFileAtomic'] = writeFileAtomic
33
76
  exports['getMode'] = getMode
34
77
  exports['fileExists'] = fileExists
35
78
  exports['dirExists'] = dirExists
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "dcp-client",
3
- "version": "5.8.5",
3
+ "version": "5.8.7",
4
4
  "dcp": {
5
- "version": "15b808e6c247df96fe9ccbedcfe66da2f1b1876b",
5
+ "version": "2d7fd6cf7cbfce1288b16fbb2e7821608cb6fba1",
6
6
  "repository": "git@gitlab.com:Distributed-Compute-Protocol/dcp.git"
7
7
  },
8
8
  "description": "Core libraries for accessing DCP network",