oscalls.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. # THIS FILE IS A PART OF VCStudio
  2. # PYTHON 3
  3. import os
  4. import time
  5. import json
  6. import datetime
  7. import platform
  8. from subprocess import *
  9. # studio
  10. from studio import history
  11. from settings import talk
  12. ostype = platform.system()
  13. def Open(arg): # XDG-OPEN (start the file in a default software)
  14. ############################################################################
  15. # This function is requested by people. I can't actually test it properly
  16. # because I don't use proprietary software. And in my opinion this function
  17. # should not even exists here.
  18. # In a GNU/Linux system to open a file with a default program you use xdg-open
  19. # that does the job for you. When talking to people I figured out that similar
  20. # functions exist on different OS as well. But unfortunatly they are all
  21. # different. It's start in Windows and open in MacOS systems. Or so I
  22. # understand. I could be wrong.
  23. # I'm not going to make sure that all xdg-open calls are done using this
  24. # function. So if you trying to run VCStudio on non GNU/Linux system please
  25. # take it into concideration. You can search for xdg-open in all files. And
  26. # change those commands to oscall.Open() instead. ( Keep in mind it has to
  27. # be imported first )
  28. # I don't condone use of non-free software.
  29. ############################################################################
  30. # For The Best OS Ever
  31. if ostype == "Linux": ##### ## ## ## ##
  32. Popen(["xdg-open", arg]) ## ## #### ## ## ##
  33. # For Stinky. Like very, very Stinky ## ## ## ## ## ##
  34. elif ostype == "Windows": ## #### ## ## ## ## ##
  35. os.system("start "+arg) ## # ## ## ## ## ## ##
  36. # For Not that Stinky but it is Stinky ## ## ## #### ## ##
  37. elif ostype == "Darwin": ##### ## ## ####
  38. os.system("open "+arg)
  39. def file_open(win, path):
  40. ############################################################################
  41. # Now you maybe asking yourself "Why is there 2 functions to open files in
  42. # a default application?". Well it's because this is a VCStudio and it's a
  43. # bit more complicated then a regular file manager as you probably could tell.
  44. # One of the big differences is that you can manage versions of Blender to
  45. # be used in a particular project. For example you have a high priority,
  46. # high value project that you rather use Blender LTS for. And not update
  47. # blender. But on the other hand you might start a test / bleading edge
  48. # project. You probably want to have mulitple Blender versions installed in
  49. # the same time. And be able to use one version in one project and the other
  50. # version in the other project.
  51. # In Blender-Organizer legacy there is a setting to change the Blender version
  52. # by providing a link to the folder of where it's installed. I'm planning to
  53. # do similar system here. And so while calling Blender files I want to use
  54. # the Blender that's in the setting and not the system installed Blender.
  55. # Maybe I will expend this feature to all kinds of file formats. It's handy
  56. # to make all opening of files through this function. So I would not need
  57. # to edit this stuff in many places later on.
  58. ############################################################################
  59. # Let's see if the file is inside the project or full path.
  60. if os.path.exists(win.project+"/"+path):
  61. path = win.project+"/"+path
  62. # Let's check if the file is a blend file. (I know I can read first line
  63. # and see if there a word BLENDER in it. But come on. )
  64. blendfile = False
  65. for bt in [".blend", ".blend1"]:
  66. if path.endswith(bt):
  67. blendfile = True
  68. # Executing the file
  69. if blendfile:
  70. Popen([get_current_blender(win), path])
  71. # Every blend file opening should be recorded to history. Because then
  72. # on multiuser, the other users should somehow know that changes were
  73. # done to the blendfile.
  74. history.record(win, path, "[Openned]")
  75. # One more thing that we perhaps ought to do here is a notify the user
  76. # if he is not running the backup creator script. Which is easily
  77. # accesable from the main window.
  78. try:
  79. with open(win.project+"/backup_data.json") as f:
  80. backup_data = json.load(f)
  81. except:
  82. backup_data = {}
  83. backup_lastcheck = backup_data.get("lastcheck", 0)
  84. # If there is at least one minute delay between the last check
  85. # and the current time. We throw a notification at the user.
  86. # ( The backup script should check the blend-files each 30
  87. # seconds. But could take another 30 actually backing them up ).
  88. if time.time() - 60 > backup_lastcheck:
  89. # Blender takes time to load. So it's important to delay the
  90. # notification a little bit.
  91. time.sleep(3)
  92. talk.alert("⚠ Backup Script Is Not Running!")
  93. else:
  94. Open(path)
  95. def ontor(win):
  96. if ".onion" in win.analytics.get("remote-server-url", ""):
  97. return True
  98. else:
  99. return False
  100. def copy_file(win, from_path, to_path, new_name=""):
  101. ############################################################################
  102. # This function exists because coping files is not as simple as reading one
  103. # and writting it to another. Blend-Files for example could have terrible
  104. # amount of linked stuff. And I can't be sure that the user will be always
  105. # using full path mode for these. And even. It's not good to do it anyway.
  106. # because you want to copy the project from one folder to another and keep
  107. # everything pretty much intact.
  108. # So this will make sure that stuff like blend file (maybe if future other
  109. # files) will be copied preserving all the linking inside.
  110. ############################################################################
  111. # Let's see if the file is inside the project or full path.
  112. if os.path.exists(win.project+"/"+from_path):
  113. from_path = win.project+"/"+from_path
  114. if os.path.exists(win.project+"/"+to_path):
  115. to_path = win.project+"/"+to_path
  116. # Now let's make sure that unless specified we are not overwritting some
  117. # existing file.
  118. if not new_name:
  119. new_name = from_path[from_path.rfind("/")+1:]
  120. count = 0
  121. while new_name in os.listdir(to_path):
  122. count = count + 1
  123. new_name = from_path[from_path.rfind("/")+1:from_path.rfind(".")]+"_"+str(count)+from_path[from_path.rfind("."):]
  124. # Now let's combine name and name
  125. to_path = to_path + "/" + new_name
  126. still = True
  127. # Let's check that we got a blend file
  128. blendfile = False
  129. for bt in [".blend", ".blend1"]:
  130. if from_path.endswith(bt):
  131. blendfile = True
  132. if blendfile and not ontor(win):
  133. # Now before we copy blend file we need to check whether Blender is even
  134. # installed.
  135. noblender = os.system(get_current_blender(win)+" -v") # It's a simple version check.
  136. # If there is no Blender installed os.system will return a value higher then 0.
  137. if not noblender:
  138. still = False
  139. # Now what we are going to do is start blender using a little expression
  140. # to force it to save file as.
  141. os.system(get_current_blender(win)\
  142. +" -b "\
  143. +from_path.replace(" ", "\ ")\
  144. +" --python-expr import\ bpy\;\ bpy.ops.wm.save_as_mainfile\(filepath=\\\""\
  145. +to_path.replace(" ", "\ ")+"\\\"\)")
  146. # Writting to history. Since each copy of a file is adding a blendfile.
  147. history.record(win, to_path, "[Added]")
  148. if still:
  149. # If it's not a blendfile we going to use this.
  150. with open(from_path, "rb") as in_file, open(to_path, "wb") as out_file:
  151. out_file.write(in_file.read())
  152. return to_path
  153. def get_current_blender(win):
  154. ############################################################################
  155. # This function is going to get the current blender version from the settings
  156. # (At the moment the feature is not implemented. So this is a placeholder.)
  157. ############################################################################
  158. if not ontor(win):
  159. return win.settings.get("blender-bash", "blender")
  160. else:
  161. return "xdg-open"