Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

r""" 

Support for (lib)GAP workspace files 

""" 

 

#***************************************************************************** 

# Copyright (C) 2017 Jeroen Demeyer <J.Demeyer@UGent.be> 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 2 of the License, or 

# (at your option) any later version. 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

 

import os 

import time 

from sage.env import DOT_SAGE, SAGE_LOCAL 

 

 

def gap_workspace_file(system="gap", name="workspace", dir=None): 

r""" 

Return the filename for the GAP workspace. 

 

INPUT: 

 

- ``system`` -- the name of the system, either ``"gap"`` or 

``"libgap"`` 

 

- ``name`` -- the kind of workspace, usually ``"workspace"`` but 

libGAP also uses other files 

 

- ``dir`` -- the directory where the workspaces should be stored. 

By default, this is ``DOT_SAGE/gap`` 

 

If needed, this creates ``dir``. It also removes any obsolete 

workspaces from ``dir``. 

 

EXAMPLES:: 

 

sage: from sage.interfaces.gap_workspace import gap_workspace_file 

sage: gap_workspace_file("foo", "bar", "/somewhere") 

'/somewhere/foo-bar-...' 

 

TESTS:: 

 

sage: from sage.env import DOT_SAGE 

sage: D = gap_workspace_file() 

sage: D.startswith(os.path.join(DOT_SAGE, "gap", "gap-workspace-")) 

True 

""" 

if dir is None: 

dir = os.path.join(DOT_SAGE, 'gap') 

 

h = hex(abs(hash(SAGE_LOCAL))) 

return os.path.join(dir, '%s-%s-%s' % (system, name, h)) 

 

 

def prepare_workspace_dir(dir=None): 

r""" 

Create and clean up the directory for GAP workspaces. 

 

INPUT: 

 

- ``dir`` -- the directory where the workspaces should be stored. 

By default, this is ``DOT_SAGE/gap`` 

 

OUTPUT: the actual workspace directory 

 

EXAMPLES:: 

 

sage: from sage.interfaces.gap_workspace import prepare_workspace_dir 

sage: prepare_workspace_dir() 

'.../gap' 

 

TESTS:: 

 

sage: prepare_workspace_dir(os.path.join(tmp_dir(), "new")) 

'.../new' 

""" 

if dir is None: 

dir = os.path.join(DOT_SAGE, 'gap') 

 

# Make sure that the workspace directory exists 

try: 

os.makedirs(dir) 

except OSError: 

if not os.path.isdir(dir): 

raise 

else: 

# Directory was created, add a README file 

with open(os.path.join(dir, 'README.txt'), 'w') as f: 

f.write("It is OK to delete all these cache files. They will be recreated as needed.\n") 

 

# Delete all gap workspaces that haven't been used in the last 

# week, to avoid needless cruft. I had an install on sage.math 

# with 90 of these, since I run a lot of different versions of 

# Sage, and it totalled 1.3GB of wasted space! See trac #4936. 

# We only do this after creating a new workspace, since this cruft 

# issue is only a problem if workspaces get created every so 

# often. We don't want to have to do this on every startup. 

now = time.time() 

for F in os.listdir(dir): 

if "workspace" in F: 

W = os.path.join(dir, F) 

try: 

age = now - os.path.getatime(W) 

if age >= 604800: # 1 week in seconds 

os.unlink(W) 

except OSError: 

# It's not a problem if W doesn't exist, everything 

# else is an error. 

if os.path.exists(W): 

raise 

 

return dir