1

I have a class that will check a users environment to find out if they have python installed, the classes purpose is to check if the user has multiple versions of python installed and to not default to three:

class CheckForPythonicVariables

  class << self

    def find_python_env_var
      py_path = []  # Results of the python env variables
      env_vars = ENV.to_h
      items = env_vars["Path"].split(";")  # Split the environment variables into an array
      items.each { |var|
        if var.to_s.include?("Python")  # Do you have python?
          py_path.push(var)
        end
      }
      py_path.each { |python|
        if python.include?("Python27")  # Python 2.7.x?
          return true
        elsif python.include?("Python3")  # Python 3.x.x?
          return false
        else
          raise "You do not have python installed"
        end
      }
    end

  end

end

Now this works, but it only works on Windows and a select few Linux OS, apparently Parrot is not one of them. Is there a way I can #split() the environment variables by anything that is not a letter? For example:

Windows env var: C:\Python27\;C:\Python27\Scripts;C:\ProgramData\Oracle\Java\javapath Parrot OS env var: /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

Note how the variables are split by either a semi-colon(;) or a colon(:), is there a way I could use the #split() function in ruby and split by anything that is not alphanumerical characters, or number? Or is there a better way to make sure that the user has python 2.7.x in their environment variables?

jww
  • 97,681
  • 90
  • 411
  • 885
User9123
  • 515
  • 2
  • 6
  • 14
  • Maybe you could simply use split twice ? Or, according to the OS : MS-DOS or Unix, you chose the right separator ? As an alternative, maybe you can use a command line/shell from Ruby to query the python version ? 'python -V' – Hugo Dec 30 '16 at 12:35
  • @Hugo Problem with the system call (`system('python --version')` I did think of that) is that if they have multiple versions of python, it will default to 3. – User9123 Dec 30 '16 at 13:01

2 Answers2

1

You can check user's current python version like the following: (source)

%x(python -c 'print __import__("sys").version_info < (2,8)') == "True\n"

But if a user install python 3 and python 2.7, and they use python 3 now. The script cannot handle this situation.

Community
  • 1
  • 1
khiav reoy
  • 1,373
  • 13
  • 14
  • That's the point of finding the variables, if they have 3 then it won't run, but if they have 2 and 3, it will create a temp variable called `python27` and run it from there. – User9123 Dec 30 '16 at 12:59
  • This also doesn't work: File "", line 1 'print ^ SyntaxError: EOL while scanning string literal false – User9123 Dec 30 '16 at 14:20
1

This regular expression matches all non-alphanumeric characters: /[^a-zA-Z0-9]/.

If you want to match all non-alphanumeric characters excluding forward-slash and backslash, use /[^a-zA-Z0-9\/\\]/.

Examples:

str = 'C:\Python27\;C:\Python27\Scripts;C:\ProgramData\Oracle\Java\javapath'
str.split /[^a-zA-Z0-9\/\\]/
# => ["C", "\\Python27\\", "C", "\\Python27\\Scripts", "C", "\\ProgramData\\Oracle\\Java\\javapath"]

str = '/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games'
str.split /[^a-zA-Z0-9\/\\]/
# => ["/usr/local/bin", "/usr/bin", "/bin", "/usr/local/games", "/usr/games"]